<%-- 
    Document   : rstpwd-confirm
    Created on : Feb 28, 2012, 10:14:47 AM
    Author     : lab
--%>

<%@page import="util.MD5Encryption"%>
<%@page import="util.MailUtil"%>
<%@page import="util.SharingUtil"%>
<%@page import="dto.User"%>
<%@page import="util.GeneratorUtil"%>
<%@page import="dao.UserDAO"%>
<%
    String userId = request.getParameter("uid");
    if (userId != null) {
        String code = request.getParameter("code");
        if (code != null) {
            UserDAO daoUser = new UserDAO();
            String randomPassword = GeneratorUtil.getRandomPassword(7);
            User user = daoUser.getUser(Integer.parseInt(userId));
            MD5Encryption md5 = new MD5Encryption();
            user.setActivationCode(code);
            user.setPassword(md5.encrypt(randomPassword));
            if (user.getActivationCode().equals(code)) {
                if (daoUser.updatePassword(user, false) > 0) {
                    MailUtil mailer = new MailUtil();
                    String subject = "Online Quiz: New Password";
                    String body = "Your username: " + user.getUsername();
                    body += ("<br/>Your password: " + randomPassword);
                    if (mailer.send(application, user.getEmail(), subject, body) > 0) {
                        //all are done successfully
                        SharingUtil.showMessage(session, "Your password is reset. Please check your email to get the new password");
                    } else {
                        SharingUtil.showMessage(session, "Please try later");
                    }
                } else {
                    SharingUtil.showMessage(session, "Invalid action");
                }
            } else {
                SharingUtil.showMessage(session, "Invalid activation code");
            }
        }
    }
%>
